package com.pshcp.patient.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CardTypeEnum;
import com.pshcp.common.enums.DoctorLevelEnum;
import com.pshcp.common.enums.ExpertTypeEnum;
import com.pshcp.common.enums.MaritalStatusEnum;
import com.pshcp.common.enums.MedicalRecordUseTypeEnum;
import com.pshcp.common.enums.PatCardTypeEnum;
import com.pshcp.common.enums.RegTypeEnum;
import com.pshcp.common.enums.RelationTypeEnum;
import com.pshcp.common.enums.ReportTypeEnum;
import com.pshcp.domain.dto.SysDictDTO;
import com.pshcp.patient.domain.dict.DictTabAO;
import com.pshcp.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = {"患者端-字典数据"})
@Slf4j
@RestController
@RequestMapping("/api/patient/dict")
public class DictAPI {

    @Autowired
    private SysDictService sysDictService;

    @ApiOperation("医生职称")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "医生职称")
    @PostMapping("/doctorLevel")
    public ResultDTO doctorLevel() {
        JSONArray jsonArray = new JSONArray();
        for(DoctorLevelEnum e : DoctorLevelEnum.values()){
            if(e.getCode() != 1){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("code", e.getCode()+"");
                jsonObject.put("name", e.getDesc());
                jsonArray.add(jsonObject);
            }
        }
        return ResultBuilder.success(jsonArray);
    }

    @ApiOperation("排班类型(是否专家)")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "排班类型(是否专家)")
    @PostMapping("/doctorExpert")
    public ResultDTO doctorExpert() {
        JSONArray jsonArray = new JSONArray();
        JSONObject all = new JSONObject();
        all.put("code", "");
        all.put("name", "全部");
        jsonArray.add(all);
        for(ExpertTypeEnum e : ExpertTypeEnum.values()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", e.getCode()+"");
            jsonObject.put("name", e.getDesc());
            jsonArray.add(jsonObject);
        }
        return ResultBuilder.success(jsonArray);
    }

    @ApiOperation("挂号类型")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "挂号类型")
    @PostMapping("/regType")
    public ResultDTO regType() {
        JSONArray jsonArray = new JSONArray();
        for(RegTypeEnum e : RegTypeEnum.values()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", e.getCode()+"");
            jsonObject.put("name", e.getDesc());
            jsonArray.add(jsonObject);
        }
        return ResultBuilder.success(jsonArray);
    }

    @ApiOperation("证件类型")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "证件类型")
    @PostMapping("/cardType")
    public ResultDTO cardType() {
        JSONArray jsonArray = new JSONArray();
        for(CardTypeEnum e : CardTypeEnum.values()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", e.getCode()+"");
            jsonObject.put("name", e.getDesc());
            jsonArray.add(jsonObject);
        }
        return ResultBuilder.success(jsonArray);
    }

    @ApiOperation("就诊人与本人关系")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "就诊人与本人关系")
    @PostMapping("/relationType")
    public ResultDTO relationType() {
        JSONArray jsonArray = new JSONArray();
        for(RelationTypeEnum e : RelationTypeEnum.values()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", e.getCode()+"");
            jsonObject.put("name", e.getDesc());
            jsonArray.add(jsonObject);
        }
        return ResultBuilder.success(jsonArray);
    }

    @ApiOperation("就诊卡类型")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "就诊卡类型")
    @PostMapping("/patCardType")
    public ResultDTO patCardType() {
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", PatCardTypeEnum.PAT_VISIT_CARD.getCode()+"");
        jsonObject.put("name", PatCardTypeEnum.PAT_VISIT_CARD.getDesc());
        jsonArray.add(jsonObject);
        return ResultBuilder.success(jsonArray);
    }

    @ApiOperation("婚姻状态")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "婚姻状态")
    @PostMapping("/maritalStatus")
    public ResultDTO maritalStatus() {
        JSONArray jsonArray = new JSONArray();
        for(MaritalStatusEnum e : MaritalStatusEnum.values()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", e.getCode()+"");
            jsonObject.put("name", e.getDesc());
            jsonArray.add(jsonObject);
        }
        return ResultBuilder.success(jsonArray);
    }

    @ApiOperation("病案打印用途")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "病案打印用途")
    @PostMapping("/medicalRecordUseType")
    public ResultDTO medicalRecordUseType() {
        JSONArray jsonArray = new JSONArray();
        for(MedicalRecordUseTypeEnum e : MedicalRecordUseTypeEnum.values()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", e.getCode()+"");
            jsonObject.put("name", e.getDesc());
            jsonArray.add(jsonObject);
        }
        return ResultBuilder.success(jsonArray);
    }

    @ApiOperation("报告类型")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "报告类型")
    @PostMapping("/reportType")
    public ResultDTO reportType() {
        JSONArray jsonArray = new JSONArray();
        for(ReportTypeEnum e : ReportTypeEnum.values()){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", e.getCode());
            jsonObject.put("name", e.getDesc());
            jsonArray.add(jsonObject);
        }
        return ResultBuilder.success(jsonArray);
    }

    @ApiOperation("字典表查询（国籍/民族/地区/退号原因/职业）")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "字典表查询（国籍/民族/地区/退号原因/职业）")
    @PostMapping("/dictTab")
    public ResultDTO dictTab(@RequestBody DictTabAO dictTabAO) {
        SysDictDTO sysDictDTO = CommonTool.copyProperties(dictTabAO, SysDictDTO.class);
        if("address".equals(sysDictDTO.getType()) && ObjectUtils.isEmpty(sysDictDTO.getParentCode())){
            sysDictDTO.setParentCode("100000");
        }
        JSONArray res = sysDictService.findDictByType(sysDictDTO);
        return ResultBuilder.success(res);
    }

    @ApiOperation("字典表查询（国籍/民族/地区/退号原因/职业）")
    @Log(source = LogSourceEnum.PATIENT_APP, type = LogTypeEnum.SELECT, desc = "字典表查询（国籍/民族/地区/退号原因/职业）")
    @PostMapping("/queryDictTab")
    public ResultDTO queryDictTab(@RequestBody DictTabAO dictTabAO) {
        SysDictDTO sysDictDTO = CommonTool.copyProperties(dictTabAO, SysDictDTO.class);
        if("address".equals(sysDictDTO.getType()) && ObjectUtils.isEmpty(sysDictDTO.getParentCode())){
            sysDictDTO.setParentCode("100000");
        }
        JSONArray res = sysDictService.findDictByType(sysDictDTO);
        return ResultBuilder.success(res);
    }
}
